/**
 * Created by swing on 2017/5/23.
 */

var server = require('api');
var _alert = require('alert');
var _clear = require('clearForm');
var orgOffice = avalon.define({

    $id : 'orgOffice',

    //科室操作
    office: {
        name: '',
        depTypeId: '',
        data: [],
        type: [],

        info: ''
    },

    newName: '',
    newDes: '',
    newType: '',//需要的科室类型ID

    //modal
    delOfficeModal: function () {

        if (orgOffice.office.info){
            $('#delOfficeModal').modal('show');
        } else{
            _alert('请先选择科室')
        }
    },

    queryOffice: function () {
        queryOffice1();
    },

    //删除科室
    delOffice: function () {

        var depId = orgOffice.office.info.depId;
        var data = {userId: avalon.vmodels.root.user.id,userToken: avalon.vmodels.root.user.token,depId:depId};


        $.ajax({
            url:server.delOffice + 'userId=' + data.userId + '&userToken=' + data.userToken + '&depId=' + data.depId,
            type:'delete',
            dataType:'json',
        })
            .done(function (res) {
                if(res.status == '9200'){
                    $('#delOfficeModal').modal('hide');
                    _alert('删除科室成功');
                    queryOffice1();
                }else{
                    _alert('删除失败' + res.description)
                }

            })
            .fail(function(res) {
                $('#teamTable').bootstrapTable();
                _alert('网络错误，请刷新尝试！');
                avalon.log(JSON.parse(res) + _.now())
            })
    },

    //添加科室
    addOffice: function () {

        var data = {
            userId: avalon.vmodels.root.user.id,
            userToken: avalon.vmodels.root.user.token,
            depTypeId: orgOffice.newType,
            depDes: orgOffice.newDes,
            depName: orgOffice.newName
        }

        if (orgOffice.newType == ''){
            _alert('请选择科室类型');
        } else {

            $.ajax({
                url:server.addOffice,
                type:'post',
                dataType:'json',
                headers: {
                    'Content-Type': 'application/json'
                },
                data:JSON.stringify(data)
            })
                .done(function (res) {
                    if(res.status == '9200'){
                        $('#addOfficeModal').modal('hide');
                        _alert('添加科室成功');
                        _clear($('#addOfficeModal'));
                        queryOffice1();
                    }else{
                        _alert(res.description)
                    }

                })
        }

    },

    init: function(){
        avalon.log("组织机构 init"+_.now());

        setTimeout(function () {

            $(function () {

                //查询科室类型
                $.ajax({
                    url: server.queryOffice,
                    type: 'get',
                    dataType: 'json',
                    data: {userId: avalon.vmodels.root.user.id,userToken:avalon.vmodels.root.user.token}
                })
                    .fail(function (res) {
                        avalon.log(res)
                    })
                    .done(function (res) {
                        if(res.status == '9200'){
                            var data = res.values;
                            orgOffice.office.type = avalon.mix(true, [], data);

                            queryOffice1();
                        }else{
                            _alert(res.description)
                        }

                    })
            })

        },100)
    }
})

//查询科室信息数据
function queryOffice1() {

    orgOffice.office.info = '';
    orgOffice.newType = '';

    $('#officeInfo').bootstrapTable({});
    var data = {
        userId: avalon.vmodels.root.user.id,
        userToken:avalon.vmodels.root.user.token,
        depName: orgOffice.office.name,
        depTypeId: orgOffice.office.depTypeId
    }

    $.ajax({
        url: server.officeInfo,
        type: 'get',
        dataType: 'json',
        data: data

    })
        .done(function (res) {
            if(res.status == '9200'){
                var data = res;
                orgOffice.office.data = data.values;
                setTimeout(function () {
                    $('#officeInfo').bootstrapTable('load',data.values);
                    $('#officeInfo').bootstrapTable('selectPage', 1);

                    $('#officeInfo').on('check.bs.table', function (e, row,ele) {
                        orgOffice.office.info = row;
                    })

                    $('#addOfficeModal').on('show.bs.modal', function (e) {
                        // do something...
                        _clear($('#addOfficeModal'));
                    })
                })
            }else {
                // _alert(res.description);
                $('#officeInfo').bootstrapTable('removeAll');
                $('#officeInfo').bootstrapTable({});
            }
        })
        .fail(function (res) {
            _alert('网络错误，请重试')
        })
}


module.exports = orgOffice;